package db

import (
	"fmt"
	"github.com/cengsin/oracle"
	"golang-learning/gorm/variable"
	"gorm.io/gorm"
	"gorm.io/gorm/logger"
	"log"
	"os"
	"time"
)

func GetOracleDbConn() (db *gorm.DB, errR error) {
	/*看是否已经有连接了*/
	if variable.GormDbOracle != nil {
		fmt.Println("Oracle连接已存在直接使用")
		db = variable.GormDbOracle
		return
	}

	/*日志*/
	newLogger := logger.New(
		log.New(os.Stdout, "", log.LstdFlags), // io writer（日志输出的目标，前缀和日志包含的内容——译者注）
		logger.Config{
			SlowThreshold:             time.Second,   // 慢 SQL 阈值
			LogLevel:                  logger.Silent, // 日志级别
			IgnoreRecordNotFoundError: true,          // 忽略ErrRecordNotFound（记录未找到）错误
			Colorful:                  false,         // 禁用彩色打印
		},
	)

	db, err := gorm.Open(oracle.Open("o2oa/JieYiSoft123@192.168.1.225:1521/devdb"), &gorm.Config{
		Logger: newLogger,
		//SkipDefaultTransaction: true,
	})

	if err != nil {
		fmt.Println(err)
		errR = err
		return
	} else {
		fmt.Println("connection succedssed")
	}

	/*连接池*/
	sqlDB, err := db.DB()
	sqlDB.SetMaxIdleConns(10)
	sqlDB.SetMaxOpenConns(100)
	sqlDB.SetConnMaxLifetime(time.Hour)

	if err != nil {
		fmt.Println(err)
		errR = err
		return
	}

	variable.GormDbOracle = db

	return
}
